//
//  YYKitViewController.m
//  Image圆角优化
//
//  Created by 宜必鑫科技 on 2017/11/20.
//  Copyright © 2017年 宜必鑫科技. All rights reserved.
//

#import "YYKitViewController.h"


@interface YYKitViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *imageView1;
@property (nonatomic, strong) UIImageView *imageView2;
@property (nonatomic, strong) UIImageView *imageView3;
@property (nonatomic, strong) UIImageView *imageView4;
@end

@implementation YYKitViewController

///> 图片加水印呢？？
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"图片处理的算法（黑白 什么的）";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self createView];
    [self setGrayImage];
    [self setColorImage];
    [self setColorImage2];
    [self setWhiteningImage];
}

///> 图片转化为byte data
- (unsigned char*)converUIImagetoData:(UIImage *)image
{
    CGImageRef imageRef = [image CGImage];
    CGSize image_size = image.size;
    ///> CGColorSpaceRef
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    ///> 分配内存
    void *data = malloc(image_size.width * image_size.height * 4);
    ///> CGBitmap
    /*
     data                                    指向要渲染的绘制内存的地址。这个内存块的大小至少是（bytesPerRow*height）个字节
     width                                  bitmap的宽度,单位为像素
     height                                bitmap的高度,单位为像素
     bitsPerComponent        内存中像素的每个组件的位数.例如，对于32位像素格式和RGB 颜色空间，你应该将这个值设为8.
     bytesPerRow                  bitmap的每一行在内存所占的比特数
     colorspace                      bitmap上下文使用的颜色空间。
     bitmapInfo                       指定bitmap是否包含alpha通道，像素中alpha通道的相对位置，像素组件是整形还是浮点型等信息的字符串。
     */
    /*
     描述：
     当你调用这个函数的时候，Quartz创建一个位图绘制环境，也就是位图上下文。当你向上下文中绘制信息时，Quartz把你要绘制的信息作为位图数据绘制到指定的内存块。一个新的位图上下文的像素格式由三个参数决定：每个组件的位数，颜色空间，alpha选项。alpha值决定了绘制像素的透明性。
     */
    CGContextRef content = CGBitmapContextCreate(data, image_size.width, image_size.height, 8, image_size.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    ///> 渲染绘图        环境
    CGContextDrawImage(content, CGRectMake(0, 0, image_size.width, image_size.height), imageRef);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(content);
    return (unsigned char*)data;
}

///> 将data转化为Image CGImageCreate
- (UIImage *)convertDataUIImage:(unsigned char*)imageData  Width:(CGFloat)width Height:(CGFloat)height
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmap = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderIntent = kCGRenderingIntentDefault;
    
    CGDataProviderRef provide = CGDataProviderCreateWithData(NULL, imageData, width * height * 4 , NULL);
    CGImageRef imageRef = CGImageCreate(width,height, 8, 32, 4 * width, colorSpace, bitmap, provide, NULL, NO, renderIntent);
    
    UIImage *imageNew = [UIImage imageWithCGImage:imageRef];
    
    CFRelease(imageRef);
    CGColorSpaceRelease(colorSpace);
    CGDataProviderRelease(provide);
    
    return imageNew;
}

- (unsigned char *)imageGrayWithData:(unsigned char*)data  Width:(CGFloat)width Height:(CGFloat)height
{
    ///> 分配内存空间 restltData指向第一位
    unsigned char * restltData = malloc(width * height * sizeof(unsigned char *) *  4);
    ///> 用0来填充
    memset(restltData, 0, width * height * sizeof(unsigned char *) *  4);
    
    ///> 进行计算
    for (int H = 0; H < height; H++) {
        for (int W = 0; W < width; W++) {
            unsigned int imageIndex = H * width + W;
            unsigned char  R = *(data + imageIndex * 4);
            unsigned char  G = *(data + imageIndex * 4 + 1);
            unsigned char  B = *(data + imageIndex * 4 + 2);
            int bitmap = R * 77 /255 + G * 151 / 255 + B * 88 /255;
//            int bitmap = (R + G + B)/3;
            int newBitMap = (bitmap > 255)? 255:bitmap;
            
            ///> 填充  RGB 将每一个[R G B A] 将RBB都改变的一样了
            memset(restltData + imageIndex * 4, newBitMap, 1);
            memset(restltData + imageIndex * 4 + 1, newBitMap, 1);
            memset(restltData + imageIndex * 4 + 2, newBitMap, 1);
        }
    }
    return restltData;
}

- (unsigned char *)imageColorWithData:(unsigned char*)data  Width:(CGFloat)width Height:(CGFloat)height
{
    ///> 分配内存空间 restltData指向第一位
    unsigned char * restltData = malloc(width * height * sizeof(unsigned char *) *  4);
    ///> 用0来填充
    memset(restltData, 0, width * height * sizeof(unsigned char *) *  4);
    
    ///> 进行计算  用计算过的data得值 替换restltData的值
    for (int H = 0; H < height; H++) {
        for (int W = 0; W < width; W++) {
            unsigned int imageIndex = H * width + W;
            
            /// 获取到原来图片imageIndex的颜色信息
            unsigned char  R = *(data + imageIndex * 4);
            memset(restltData + imageIndex * 4, 255 - R, 1);

            unsigned char  G = *(data + imageIndex * 4 + 1);
            memset(restltData + imageIndex * 4 + 1, 255 - G, 1);

            unsigned char  B = *(data + imageIndex * 4 + 2);
            memset(restltData + imageIndex * 4 + 2, 255 - B, 1);
        }
    }
    return restltData;
}

- (unsigned char *)imageWhiteningWithData:(unsigned char*)data  Width:(CGFloat)width Height:(CGFloat)height
{
    ///> 分配内存空间 restltData指向第一位
    unsigned char * restltData = malloc(width * height * sizeof(unsigned char *) *  4);
    ///> 用0来填充
    memset(restltData, 0, width * height * sizeof(unsigned char *) *  4);
    
    ///> 将色值进行一一对应
    NSArray *array = @[@"0",@"55",@"110",@"155",@"185",@"220",@"240",@"250",@"255"];
    NSMutableArray *muArray = [NSMutableArray array];
    for (int i = 0; i < 8; i++) {
        
        NSString *str = array[i];
        NSString *str1 = array[i + 1];
        int value = str.intValue;
        int value1 = str1.intValue;
        float mapValue = (value1 - value) / 32.0;

        for (int y = 0; y < 32; y++) {
            [muArray addObject:@((int)(mapValue * y + value))];
        }
    }
    
    ///> 进行计算  用计算过的data得值 替换restltData的值
    for (int H = 0; H < height; H++) {
        for (int W = 0; W < width; W++) {
            unsigned int imageIndex = H * width + W;
            
            /// 获取到原来图片imageIndex的颜色信息
            unsigned char  R = *(data + imageIndex * 4);
            NSNumber *rNum = muArray[R];
            memset(restltData + imageIndex * 4, rNum.intValue, 1);
            
            unsigned char  G = *(data + imageIndex * 4 + 1);
            NSNumber *gNum = muArray[G];
            memset(restltData + imageIndex * 4 + 1, gNum.intValue, 1);
            
            unsigned char  B = *(data + imageIndex * 4 + 2);
            NSNumber *bNum = muArray[B];
            memset(restltData + imageIndex * 4 + 2, bNum.intValue, 1);
        }
    }
    return restltData;
}







- (void)createView
{
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, kScreenW/2 - 15, (kScreenW/2 - 15)*0.5156)];
    _imageView.image = [UIImage imageNamed:@"ss(4)"];
    [self.view addSubview:_imageView];
    _imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW/2 + 5, 10, kScreenW/2 - 15, (kScreenW/2 - 15)*0.5156)];
    _imageView1.image = [UIImage imageNamed:@"ss(4)"];
    [self.view addSubview:_imageView1];
    _imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, kScreenW/2, kScreenW/2 - 15, (kScreenW/2 - 15)*0.5156)];
    _imageView2.image = [UIImage imageNamed:@"ss(4)"];
    [self.view addSubview:_imageView2];
    _imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW/2 + 5,  kScreenW/2, kScreenW/2 - 15, (kScreenW/2 - 15)*0.5156)];
    _imageView3.image = [UIImage imageNamed:@"ss(4)"];
    [self.view addSubview:_imageView3];
    _imageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(10, kScreenW, kScreenW/2 - 15, (kScreenW/2 - 15)*0.5156)];
    _imageView4.image = [UIImage imageNamed:@"ss(4)"];
    [self.view addSubview:_imageView4];
}

- (void)setGrayImage
{
    UIImage *image = [UIImage imageNamed:@"ss(4)"];
    unsigned char * imageData = [self converUIImagetoData:image];
    unsigned char * newImageData = [self imageGrayWithData:imageData Width:image.size.width Height:image.size.height];
    _imageView1.image = [self convertDataUIImage:newImageData Width:image.size.width Height:image.size.height];
}

- (void)setColorImage
{
    UIImage *image = [UIImage imageNamed:@"ss(4)"];
    unsigned char * imageData = [self converUIImagetoData:image];
    unsigned char * newImageData = [self imageColorWithData:imageData Width:image.size.width Height:image.size.height];
    _imageView2.image = [self convertDataUIImage:newImageData Width:image.size.width Height:image.size.height];
}


- (void)setColorImage2
{
    UIImage *image = [UIImage imageNamed:@"ss(4)"];
    unsigned char * imageData = [self converUIImagetoData:image];
    unsigned char * grayImageData = [self imageGrayWithData:imageData Width:image.size.width Height:image.size.height];
    unsigned char * newImageData = [self imageColorWithData:grayImageData Width:image.size.width Height:image.size.height];
    _imageView3.image = [self convertDataUIImage:newImageData Width:image.size.width Height:image.size.height];
}

- (void)setWhiteningImage
{
    UIImage *image = [UIImage imageNamed:@"aobama.jpg"];
    unsigned char * imageData = [self converUIImagetoData:image];
    unsigned char * newImageData = [self imageWhiteningWithData:imageData Width:image.size.width Height:image.size.height];
    _imageView4.image = [self convertDataUIImage:newImageData Width:image.size.width Height:image.size.height];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self dismissViewControllerAnimated:YES completion:NULL];
}




@end
